<template>
  <div class="patientManage">
    <div class="topSearch">
      <div class="s-left">
        <el-input class="s-ipt" placeholder="请输入姓名" prefix-icon="el-icon-search" v-model.trim="tableData.listInfo1.form.name" @change="searchTab"></el-input>
        <!-- <el-button type="text">更多搜索<i class="el-icon-arrow-down el-icon--right"></i></el-button> -->
      </div>
      <div class="s-btns">
        <el-button type="primary" v-if="userType !==1" @click="pop.pop1.title = '添加用户';pop.pop1.flag = true" v-authority>新增</el-button>
      </div>
    </div>
    <!-- <div class="highSearch">
      <el-row :gutter="20">
        <el-col :span="8">fasdsdafd--</el-col>
      </el-row>
    </div> -->
    <div class="tableMain" v-if="tableData.listInfo1.list.length">
      <!-- <el-table :data="tableData.listInfo1.list" stripe fixed :height="tableData.listInfo1.height" style="width: 100%"> -->
      <el-table :data="tableData.listInfo1.list" ref="eltable" stripe fixed height="100%" style="width: 100%">
        <el-table-column fixed="left" label="序号" width="80">
          <template slot-scope="scope">
            {{ (tableData.listInfo1.form.pageNum - 1) * tableData.listInfo1.form.pageSize + scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip :prop="KEYS.key1" label="账号"></el-table-column>
        <el-table-column show-overflow-tooltip :prop="KEYS.key2" label="姓名"></el-table-column>
        <el-table-column show-overflow-tooltip :prop="KEYS.key6" label="用户类型">
          <template slot-scope="scope" width="90">
            <div v-if="scope.row.type ===0">管理员</div>
            <div v-else-if="scope.row.type ===1">医师</div>
            <div v-else-if="scope.row.type ===2">工程师</div>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip :prop="KEYS.key7" label="所属科室">
          <template slot-scope="scope">
            <div v-if="scope.row.type==1">
              <div v-if="scope.row.hospital != null">{{scope.row.hospital+'--'+scope.row.department}}</div>
              <div v-else>暂无</div>
            </div>
            <div v-else>其他</div>

          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="150" v-if="userType !== 1 && userType !== 2">
          <template slot-scope="scope">
            <!--<a v-if="userType !==1" style="cursor:pointer;" @click="editFormPatient(scope.row)">修改密码</a>-->
            <!--<a v-if="userType !==1" style="cursor:pointer;margin-left:6px;" @click="deletFormPatient(scope.row)">删除</a>-->
            <el-tooltip effect="light" content="重置密码" placement="left">
              <i class="handleBtn amsp el-icon-lock" v-if="userType !== 1 && userType !== 2" @click="passwordInit(scope.row)"></i>
            </el-tooltip>
            <el-tooltip effect="light" content="删除" placement="left">
              <i class="handleBtn amsp el-icon-delete" v-if="userType !== 1 && userType !== 2" @click="deletFormPatient(scope.row)"></i>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <amsp-page
        tableName="listInfo1"
        :pageSize="tableData.listInfo1.form.pageSize"
        @changeSize="changeSize($event, 'listInfo1')"
        @setPage="changePage($event, 'listInfo1')"
        :tableForm="tableData.listInfo1.form"
        @toFirst="changePage(1, 'listInfo1')"
        @toEnd="changePage(tableData.listInfo1.form.pages, 'listInfo1')"/>
    <amsp-pop class="addEditPatient" ref="addEditPatient"  :title="pop.pop1.title" width="560px":visible.sync="pop.pop1.flag" :before-close="closeAddEdit">
      <div class="addEditPage">
        <el-form class="addEditFormPatient" ref="addEditFormPatient" :model="pop.pop1.form" :rules="RULES"  label-position="left" label-width="80px" @submit.native.prevent>
          <el-form-item label="账号" :prop="KEYS.key1">
            <el-input placeholder="请输入账号" v-model.trim="pop.pop1.form[KEYS.key1]"></el-input>
          </el-form-item>
          <el-form-item label="姓名" :prop="KEYS.key2">
            <el-input placeholder="请输入姓名" v-model.trim="pop.pop1.form[KEYS.key2]"></el-input>
          </el-form-item>
          <el-form-item label="用户类型" :prop="KEYS.key6">
            <el-select v-model="pop.pop1.form[KEYS.key6]" placeholder="请选择用户类型">
              <el-option
                  v-for="item in arr_1"
                  :key="item.id"
                  :label="item.title"
                  :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="所属科室" :prop="KEYS.key7" v-if="pop.pop1.form.type ===1">
            <el-select v-model="pop.pop1.form[KEYS.key7]" clearable filterable placeholder="请选择所属科室" >
              <el-option
                  v-for="item in arr_2"
                  :key="item.id"
                  :label="item.hospital+'--'+item.department"
                  :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div class="baseFormBtns" id="baseFormBtns">
          <el-button round @click="closeAddEdit">取消</el-button>
          <el-button round type="primary" @click="addUser">确定</el-button>
        </div>
      </div>
    </amsp-pop>
  </div>
</template>

<script>
  import {queryUserManagePage, editPassWord, addUser,delUser,passwordInit} from '@/depends/api/user'
  import {queryDepartmentPage} from '@/depends/api/department'
  import {deepCopy} from '@/utils/tools/tools'
  import {KEYS_RULES_MIXIN, KEYS} from './component/keys.js'
  import {mapGetters} from 'vuex'
  export default {
    components: {
    },
    mixins: [KEYS_RULES_MIXIN],
    // provide () {
    //   return {
    //     vMain: this
    //   }
    // },
    data() {
      return {
        pop: {
          pop1: {
            flag: false,
            title: '添加用户',
            form: {
              [KEYS.key0]: '',
              [KEYS.key1]: '',
              [KEYS.key2]: '',
              [KEYS.key3]: '123456',
              [KEYS.key6]: 1,
              [KEYS.key7]: 1,
            }
          }
        },
        tableData: {
          listInfo1: {
            loading: false,
            form: {
              name: '',

              pageNum: 1,
              pageSize: 15,
              total: 30,
              pages: 1,
            },
            list: []
          }
        },
        userType:1,
        arr_1:[
          {id:1,title:'医师'},
          {id:2,title:'工程师'}
        ],
        arr_2:[]
      };
    },
    computed:{
      ...mapGetters('user', ['getUserType']),
    },
    created() {
      this.userType = this.getUserType;
      if(this.userType === 2){
        this.arr_1 = [{id:1,title:'医生'}];
      }
      this.queryUserManagePage();
      //查询所有科室
      this.queryDepartmentPage();
    },
    mounted() {

    },
    methods:{
      deletFormPatient (a) {
        this.$confirm('确认删除?', '提示', {
          confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => {
          delUser(a.id).then(res => {
            if (res.data.status !== 200) { return }
            this.$message.success('删除成功!')
            this.queryUserManagePage()
          })
        }).catch(() => {
          // this.$message({
          //   type: 'info',
          //   message: '已取消删除'
          // });
        });
      },
      passwordInit(a){
        passwordInit(a.id).then(res => {
          if (res.data.status !== 200) { return }
          this.$message.success('密码重置成功!')
        }).catch(() =>{
          console.log('error');
        });


      },
      searchTab () {
        this.changePage(1, 'listInfo1')
      },
      changeSize (a,b) {
        this.tableData[b].form.pageSize = a
        this.changePage(1, b)
      },
      changePage (a,b) {
        this.tableData[b].form.pageNum = a
        this.queryUserManagePage()
      },
      queryUserManagePage () {
        let params = deepCopy(this.tableData.listInfo1.form)
        let postdata = {...params,type:this.userType};
        this.tableData.listInfo1.loading = true
        queryUserManagePage(postdata).then(res => {
          this.tableData.listInfo1.loading = false
          if (res.data.status !== 200) { return }
          if(res.data.data == null) {
            this.tableData.listInfo1.list =[];
            return
          }
          this.tableData.listInfo1.list = res.data.data.list
          this.tableData.listInfo1.form.pageNum = res.data.data.pageNum
          this.tableData.listInfo1.form.total = res.data.data.total
          this.tableData.listInfo1.form.pages = res.data.data.pages
          this.$nextTick(() => {
            this.$refs.eltable.bodyWrapper.scrollTop = 0
          })
        })
      },
      queryDepartmentPage () {
        let params = {pageNum:1,pageSize:1000};
        queryDepartmentPage(params).then(res => {
          if (res.data.status !== 200) { return }
          this.arr_2 = res.data.data.list;
        })
      },
      editFormPatient (a) {
        this.$router.push({name: 'modifyPWD', params: {userInfo: a}})
      },
      isPass(form) {
        if(this.pop.pop1.form.type === 1 && this.pop.pop1.form.hospitalDepartment === ''){
          this.$message.error('请选择所属科室');
          return;
        }
        return new Promise((reslove, reject) => {
          this.$refs[form].validate((valid) => {
            reslove(valid);
          });
        });
      },
      closeAddEdit () {
        this.$refs.addEditFormPatient.resetFields();
        this.pop.pop1.form[KEYS.key0] = ''
        this.pop.pop1.flag = false
      },
      async addUser () {
        let isPass = await this.isPass('addEditFormPatient');
        if (!isPass) {return}
        let params = deepCopy(this.pop.pop1.form)
        if (params.id) {
          editPatient(params).then(res => {
            if (res.data.status !== 200) { return }
            this.$message.success('编辑成功!')
            this.closeAddEdit()
            this.queryUserManagePage()
          })
        } else {
          addUser(params).then(res => {
            if (res.data.status !== 200) { return }
            this.$message.success('新增成功!')
            this.closeAddEdit()
            this.queryUserManagePage()
          })
        }
      },
      goForm () {

      }
    },
    beforeDestroy() {
    },

  }
</script>

<style scoped lang="less">
  .patientManage {
    height: 100%;
    padding: 20px;
    display: flex;
    flex-direction: column;
    padding-bottom: 65px;
    .tableMain {
      flex: 1;
    }
    .addEditPatient {
      #baseFormBtns.baseFormBtns {
        position: initial;
      }
    }
  }
</style>